package com.ruoyi.project.opencv.test.image;

import com.ruoyi.common.utils.TypeConversion;
import com.ruoyi.common.utils.file.FileUtils;

import javax.imageio.stream.FileImageOutputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;

/**
 * @program: neural
 * @ClassName HexImage
 * @description:
 * @author: dx
 * @create: 2021-07-22 10:32
 * @Version 1.0
 **/
public class HexImage {
    /**
     * 十六进制字节转字节数组
     * Creates byte array representation of HEX string.<br>
     * http://www.cnblogs.com/qgc88
     * @param s
     *            string to parse
     * @return
     */
    public static byte[] fromHexString(String s) {
        int length = s.length() / 2;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = (byte) ((Character.digit(s.charAt(i * 2), 16) << 4) | Character
                    .digit(s.charAt((i * 2) + 1), 16));
        }
        return bytes;
    }

    //byte数组转图片 http://www.cnblogs.com/qgc88

    /**
     * Creates HEX String representation of supplied byte array.<br/>
     * Each byte is represented by a double character element from 00 to ff
     * 实现字节数组向十六进制的转换方法一 http://www.cnblogs.com/qgc88
     * @param fieldData
     *            to be tringed
     * @return
     */
    public static String toHexString(byte[] fieldData) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldData.length; i++) {
            int v = (fieldData[i] & 0xFF);
            if (v <= 0xF) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString();
    }
    public static void byte2image(byte[] data,String path){
        if(data.length<3||path.equals("")) return;
        try{
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch(Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {

/********************图片转十六进制******************************/
        try
        {
            /*StringBuffer sb = new StringBuffer();
            FileInputStream fis = new FileInputStream("C:\\Users\\dx\\Desktop\\send.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);
            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1)
            {
                bos.write(buff, 0, len);
            }*/
            // 得到图片的字节数组
            byte[] result = FileUtils.getBytesByFile("C:\\Users\\admin\\Desktop\\image.png");

            System.out.println("图片字节数："+result.length);
            System.out.println("++++" + TypeConversion.bytes2HexString(result));
            // 字节数组转成十六进制

//            FileUtils.getFileByBytes(TypeConversion.hexString2Bytes(TypeConversion.bytes2HexString(result)),"C:\\Users\\admin\\Desktop\\","save.png");

            String encryptData = TypeConversion.bytes2HexString(result);
            //写入指定文件中
            BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\admin\\Desktop\\data.txt"));
            bw.write(encryptData);
            bw.close();
            /*String str = toHexString(result);
            String str = byte2HexStr(result);
            *//* http://www.cnblogs.com/qgc88
             * 将十六进制串保存到txt文件中
             *//*
            PrintWriter pw = new PrintWriter(
                    new FileWriter("16image.txt"));
            pw.println(str);
            pw.close();*/
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
